package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.entity.UserRole;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Override
    public PageResponse<RoleVo> getByPage(RoleDto roleDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Role role = BeanUtil.toBean(roleDto, Role.class);
        Page<RoleVo> page = roleMapper.getByPage(role);
        PageResponse<RoleVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    @Override
    public Set<String> getById(Long roleId) {
        return roleMapper.getById(roleId);
    }

    @Override
    public List<RoleVo> list() {
        List<Role> list = roleMapper.list();
        return BeanUtil.copyToList(list, RoleVo.class);
    }

    @Override
    public void delete(Long roleId) {
        Role role = roleMapper.selectByPrimaryKey(roleId);
        if(ObjectUtil.equal(role.getDataState(), SuperConstant.DATA_STATE_1)){
            roleMapper.deleteByPrimaryKey(roleId);
        }
    }

    @Override
    public Boolean update(RoleDto roleDto) {
        Role targetRole = roleMapper.selectByPrimaryKey(roleDto.getId());
        if(ObjectUtil.equals(targetRole.getDataState(), SuperConstant.DATA_STATE_0)&&ObjectUtil.equals(roleDto.getDataState(), SuperConstant.DATA_STATE_1))
        {
            List<UserRole> list = userRoleMapper.findByRoleId(roleDto.getId());
            if(ObjectUtil.isNotEmpty(list)){
                throw new RuntimeException("该角色已分配用户，不能禁用");
            }
        }
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.updateByPrimaryKeySelective(role);
        if (ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())) {
            return true;
        }
        roleResourceMapper.deleteRoleResourceByRoleId(role.getId());

        List<RoleResource> roleResourceList = new ArrayList<>();
        String[] checkedResourceNos = roleDto.getCheckedResourceNos();
        List<String> resourceNoList = Arrays.asList(checkedResourceNos);
        resourceNoList.forEach(r->{
            RoleResource roleResource = RoleResource.builder()
                    .roleId(role.getId())
                    .resourceNo(r)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            roleResourceList.add(roleResource);
        });
        roleResourceMapper.batchInsert(roleResourceList);
        return true;
    }

    @Override
    public void add(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);
    }
}
